//
//  HuanKuanPageViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/10/12.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "HuanKuanPageViewController.h"
#import "JTCSM.h"
#import "MyDoubleLabelView.h"
#import "CustomPageControl.h"
#import "MyBankCardView.h"
#import "InfoView.h"
#import "AddPlanViewController.h"
#import "MyBankCardModel.h"
#import "SeePlanViewController.h"
#import "HistoryPlanViewController.h"
#import "AddBankViewController.h"

#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "MKJCollectionViewCell.h"

#import "RegisterVIew.h"
#import "HuanKuanJiluViewController.h"
#import "LiJiHuanKuanViewController.h"
#import "NewHuanKuanPlanViewController.h"
#define KWidthOfRegister 50

@interface HuanKuanPageViewController () <TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>
{
    NSInteger _selectedIndex;
    NSMutableArray *titlearray;
    RegisterVIew * registerV;
    NSArray *placeholderArray;
    NSInteger idx;
    MyBankCardModel *my_model;
}
@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYPageControl *pageControl;
@property (nonatomic, strong) NSArray *datas;


@end

@implementation HuanKuanPageViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self theRequest];


}
- (void)gotoAddBankVC{
    AddBankViewController *vc = [[AddBankViewController alloc]init];
    vc.nameTitle = @"添加信用卡";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor =[UIColor whiteColor];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    self.navigationItem.rightBarButtonItem = [leftbtn setBarButtonItemsTitle:@"添加" frame:KRightBarButtonItemFrame_text target:self SEL:@selector(gotoAddBankVC)];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(gotoHuankuanjilu) name:@"gotoJiluVC" object:nil];
    [self addPagerView];
    [self addPageControl];
    [self layoutview];
    
    UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftbutton.frame = CGRectMake(0, KCONTROLLERHeight-50-KagainChangeHeight, KCONTROLLERWith/2, 50);
    leftbutton.backgroundColor = KZhuTiRGB;
    [leftbutton setTitle:@"立即还款" forState:UIControlStateNormal];
    [leftbutton setTitleColor:RGBACOLOR(51, 51, 51, 1) forState:UIControlStateNormal];
    [self.view addSubview:leftbutton];
    [leftbutton addTarget:self action:@selector(gotoAddPlanVC:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(KCONTROLLERWith/2, KCONTROLLERHeight-50-KagainChangeHeight, KCONTROLLERWith/2, 50);
    rightButton.backgroundColor = RGBACOLOR(51, 51, 51, 1);
    [rightButton setTitle:@"还款计划" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:rightButton];
    [rightButton addTarget:self action:@selector(gotoSeePlanVC:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
- (void)layoutview{
    idx = 0;
    CGFloat imagesize = 15;
    placeholderArray=@[@"还款记录",@"同步账单"];
    for (int i = 0; i < placeholderArray.count; i++) {
        idx = i;
        registerV = [[RegisterVIew alloc]initWithFrame:CGRectMake(0,234+i*KWidthOfRegister, self.view.frame.size.width, KWidthOfRegister) labeTtext:nil placeholder:@""];
        registerV.userInteractionEnabled =YES;
        registerV.tag = 1000+i;
        registerV.label.textAlignment = NSTextAlignmentLeft;
        [self.view addSubview:registerV];
        [self addImageWith:registerV];
        registerV.textField.enabled = NO;
        
        UIImageView *imageView  = [[UIImageView alloc]initWithFrame:CGRectMake(KCONTROLLERWith-imagesize-13,  (registerV.frame.size.height-imagesize)/2, imagesize, imagesize)];
        imageView.image = [UIImage imageNamed:@"myFanhui.png"];
        [registerV addSubview:imageView];
        registerV.textField.frame = CGRectMake(registerV.textField.frame.origin.x, registerV.textField.frame.origin.y, registerV.textField.frame.size.width-imagesize-20, registerV.textField.frame.size.height);
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoHistoryPlanVC:) andaddView:registerV];
        
        if (i == 1 ) {
            registerV.textField.text = @"马上同步";
            registerV.textField.textColor = RGBACOLOR(54, 54, 54, 1);
        }else{
            registerV.textField.text = @"";
        }
        
    }
}
#pragma mark --添加灰线条
-(void)addImageWith:(UIView *)fatherview{
    
    UILabel *lable =[[UILabel alloc] initWithFrame:CGRectMake(20,  0, 77, KWidthOfRegister-1.5)];
    [lable setText:placeholderArray[idx++]];
    lable.textAlignment=YES;
    lable.textAlignment = NSTextAlignmentLeft;
    lable.textColor = RGBACOLOR(54, 54, 54, 1);
    lable.font =[UIFont fontWithName:@"Arial" size:15.0];
    [fatherview addSubview:lable];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, fatherview.frame.size.height-1.5, KCONTROLLERWith, 1.5)];
    view.backgroundColor = RGBACOLOR(226, 227, 228,1);
    [fatherview addSubview:view];
    
    
}

- (void)addPagerView {
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 200);
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[MKJCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [self.view addSubview:pagerView];
    _pagerView = pagerView;
    
}

- (void)addPageControl {
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    pageControl.frame = CGRectMake(0, CGRectGetHeight(_pagerView.frame) - 0, CGRectGetWidth(_pagerView.frame), 26);
    pageControl.currentPageIndicatorSize = CGSizeMake(8, 8);
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = KZhuTiRGB;
    [_pagerView addSubview:pageControl];
    _pageControl = pageControl;
    
}


- (void)loadData {
    
    _datas = [@[titlearray[0]] copy];
    _pageControl.numberOfPages = _datas.count;
    [_pagerView reloadData];
}

#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return _datas.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    MKJCollectionViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    [cell setvalueWithModel:_datas[index] indexPath:nil];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.93, CGRectGetHeight(pageView.frame)*0.93);
    layout.itemSpacing =0;
//    layout.minimumAlpha = 0.3;
    layout.itemHorizontalCenter = NO;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
    my_model = _datas[toIndex];
}



- (void)gotoAddPlanVC:(UIButton *)sender{
    LiJiHuanKuanViewController *vc = [[LiJiHuanKuanViewController alloc]init];
    vc.nameTitle = @"立即还款";
//    vc.card_id = my_model.card_id;
//    vc.huanri = my_model.bill_date;
//    vc.zhangdanri = my_model.repay_date;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)gotoSeePlanVC:(UIButton *)sender{
    NewHuanKuanPlanViewController *vc = [[NewHuanKuanPlanViewController alloc]init];
    vc.nameTitle = @"还款计划";
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)gotoHuankuanjilu{
    HuanKuanJiluViewController *vc = [[HuanKuanJiluViewController alloc]init];
    vc.nameTitle = @"还款计划";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)gotoHistoryPlanVC:(UITapGestureRecognizer *)sender{

    RegisterVIew *view = (RegisterVIew *)[sender view];
    if (view.tag == 1000) {
        HuanKuanJiluViewController *vc = [[HuanKuanJiluViewController alloc]init];
        vc.nameTitle = @"还款计划";
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        HistoryPlanViewController *vc = [[HistoryPlanViewController alloc]init];
        vc.nameTitle = @"历史计划列表";
        vc.card_id = my_model.card_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)theRequest{
    [titlearray removeAllObjects];
    titlearray = nil;
    titlearray = [NSMutableArray array];
    
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"CreditCard/index") withDict:@{@"token":KTokenString} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSArray *bannerArr = KResultOfKey(@"data");
            [self->titlearray removeAllObjects];
            for (NSDictionary *bannerdict in bannerArr) {
                MyBankCardModel *model = [MyBankCardModel whc_ModelWithJson:bannerdict];
                [self->titlearray addObject:model];
                self->my_model = self->titlearray[0];
            }
            
            [self loadData];
            self->_pagerView.isInfiniteLoop =YES;
            self->_pagerView.autoScrollInterval =0;
            self->_pagerView.layout.itemHorizontalCenter = NO;
            self->_pagerView.layout.layoutType = TYCyclePagerTransformLayoutLinear;
            self->_pagerView.layout.itemSize = CGSizeMake(CGRectGetWidth(self->_pagerView.frame)*0.93, CGRectGetHeight(self->_pagerView.frame)*0.93);
            self->_pagerView.layout.itemSpacing =0;
            [self->_pagerView setNeedUpdateLayout];
            
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
